package com.iceland.common.rpc;

import com.iceland.common.message.CmdMessage;
import com.iceland.common.message.MessageType;
import com.iceland.common.strategy.CmdStrategy;
import com.iceland.common.thread.MessageEventExcutor;
import com.iceland.common.util.AttributeKeyUtils;
import com.iceland.common.util.ChannelContext;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;


/**
 * 远程方法调用响应
 */
@Slf4j
public class MethodInvocationResponseStrategy implements CmdStrategy {
    @Override
    public void doStrategy(ChannelHandlerContext ctx, CmdMessage m) {
        MessageEventExcutor.getInstance().submit(new Runnable() {
            @Override
            public void run() {
                try {
                    //反序列化
                    RpcResponse response=m.getData();
                    //写入响应，并唤醒阻塞线程
                    ChannelContext channelContext = AttributeKeyUtils.getChannelContext(ctx.channel());
                    RpcMessageManager rpcMessageManager = channelContext.getRpcMessageManager();
                    rpcMessageManager.response(response);

                }catch (Throwable e){
                    log.error("MethodInvocationResponseStrategy执行失败",e);
                }
            }
        });



    }

    @Override
    public int getType() {
        return MessageType.METHOD_INVOKE_RESULT.getType();
    }
}